package com.energyfuture.auth.utils;

import org.apache.commons.lang3.StringUtils;

import javax.servlet.http.HttpServletRequest;
import java.net.InetAddress;
import java.net.UnknownHostException;

/**
 * IP地址转化
 * @author wanxin
 *
 */
public class IpUtil {
	
	/**
	 * @Description 获取客户端真实ip
	 * @param request
	 * @return 请求ip
	 * @author wanxin
	 * @data 2016年11月21日 下午5:10:22
	 */
	public static String getIpAddr(HttpServletRequest request) { 
		//TODO 经过网关转发后获取ip地址
		String header = request.getHeader("client-ip");
		String ip = StringUtils.isBlank(header) ? request.getRemoteAddr() : header;
		if ("127.0.0.1".equals(ip) || "0:0:0:0:0:0:0:1".equals(ip)) {
			// 根据网卡取本机配置的IP
			try {
				ip = InetAddress.getLocalHost().getHostAddress();
			} catch (UnknownHostException e) {
				e.printStackTrace();
			}
		}
		return ip;
	}
}
